package com.example.day2;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

public class MainActivity3 extends View {
    private int width=3;
    private int color= Color.RED;
    private Paint paint=new Paint();
    private int budu;
    private int time;
    public MainActivity3(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //获取外部传入的参数值


        //描边，FIle填充
        paint.setStyle(Paint.Style.STROKE);
        //设置颜色
        paint.setColor(color);
        //抗锯齿
        paint.setAntiAlias(true);
        //字体大小

        //宽度
        paint.setStrokeWidth(width);


    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paintText = new Paint();
        paintText.setColor(Color.BLACK);
        paintText.setStyle(Paint.Style.FILL);
        paintText.setTextSize(40);
        canvas.drawText(time+"秒",30,75,paintText);
        RectF rectF = new RectF(0, 0, 120, 120);
        //canvas.drawRect(rectF,paint);
        canvas.drawArc(rectF,0,budu,false,paint);
    }
    //开始绘画
    public void start(int second){
        ValueAnimator animator = ValueAnimator.ofInt(second, 0);
        animator.setDuration(second*1000);//动画时长
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                time = (int) animation.getAnimatedValue();
                invalidate();//重新画
            }
        });
        animator.setInterpolator(new LinearInterpolator());
        animator.start();
        //改变弧度
        ValueAnimator animator2 = ValueAnimator.ofInt(0, 360);
        animator2.setDuration(second*1000);//动画时长
        animator2.setInterpolator(new LinearInterpolator());
        animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                budu = (int) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator2.start();
    }
}